import React, {Component} from 'react'


export default class App extends Component {
    state = {
        count: 0
    };

    increment = () => {
        const num = this.select.value * 1;
        const count = this.state.count + num;
        this.setState({count});
    };

    decrement = () => {
        const num = this.select.value * 1;
        const count = this.state.count - num;
        this.setState({count});
    };

    incrementIfOdd = () => {
        const num = this.select.value * 1;
        if (this.state.count % 2 === 1) {
            const count = this.state.count + num;
            this.setState({count});
        }
    };

    incrementAsync = () => {
        const num = this.select.value * 1;
        const {count} = this.state;
        setTimeout(() => {
            this.setState({count: count + num});
        }, 1000);
    };

    render() {
        const {count} = this.state;
        return (
            <div>
                <p>click {count} times</p>
                <div>
                    <select ref={select => this.select = select}>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>
                    &nbsp;&nbsp;
                    <button onClick={this.increment}>&nbsp;+&nbsp;</button>
                    <button onClick={this.decrement}>&nbsp;-&nbsp;</button>
                    <button onClick={this.incrementIfOdd}>increment if odd</button>
                    <button onClick={this.incrementAsync}>increment async</button>
                </div>
            </div>
        )
    }
}

